$(function () {
    initDg();
});

function initDg() {
    $('#dg').datagrid({
        width: '100%',
        height: 'auto',
        url: '/warSituationInfo/list',
        fitColumns: true,
        autoRowHeight: false,
        striped: true,
        method: "GET",
        idField: "fdId",
        loadMsg: "正在加载数据...",
        pagination: true,
        rownumbers: true,
        pagePosition: 'bottom',
        pageSize: 50,
        pageList: [20, 30, 40, 50],
        showFooter: true,
        fit: true,
        queryParams: {
            name: $('#username').val()
        },
        columns: [[
            {field: 'fdId', title: 'ID', width: 100, align: 'center', checkbox: true},
            {
                field: 'fdWarName',
                title: '战队名称',
                width: 100
            },
            {
                field: 'fdNum',
                title: '参战人数',
                width: 100
            },
            {
                field: 'fdAmount',
                title: '下注金额',
                width: 100
            },
            {
                field: 'fdChannel',
                title: '授权通道',
                width: 100
            },
            {
                field: 'fdWaitTime',
                title: '等待时间',
                width: 100
            },
            {
                field: 'fdStartTime',
                title: '游戏开始时间',
                width: 100
            },
            {
                field: 'fdRoomNo',
                title: '房间号',
                width: 100
            },
            {
                field: 'fdCreateName',
                title: '发布者',
                width: 100
            },
            {
                field: 'fdCreatePic',
                title: '发布者截图',
                width: 100,
                formatter: function (value) {
                    return "<a href='javascript:void(0)' onclick=\"showPic('" + value + "')\">点击查看</a>"
                }
            },
            {
                field: 'fdCreateResult',
                title: '发布者判定',
                width: 100,
                formatter: function (value, row, index) {
                    if (value === 0) {
                        return "发布者胜"
                    } else if (value === 1) {
                        return "挑战者胜";
                    }
                }
            },
            {
                field: 'fdDareName',
                title: '挑战者',
                width: 100
            },
            {
                field: 'fdDarePic',
                title: '挑战者截图',
                width: 100,
                formatter: function (value) {
                    return "<a href='javascript:void(0)' onclick=\"showPic('" + value + "')\">点击查看</a>"
                }
            },
            {
                field: 'fdDareResult',
                title: '挑战者判定',
                width: 100,
                formatter: function (value, row, index) {
                    if (value === 0) {
                        return "发布者胜"
                    } else if (value === 1) {
                        return "挑战者胜";
                    }
                }
            },
            {
                field: 'fdPublishDesc',
                title: '发布者简介',
                width: 100
            },
            {
                field: 'fdDareDesc',
                title: '挑战者简介',
                width: 100
            },
            {
                field: 'fdIsEnd',
                title: '战局是否结束',
                formatter: function (value, row, index) {
                    if (value === 1) {
                        return "已结束";
                    }
                    else {
                        return "未结束";
                    }
                }
            },
            {
                field: 'fdIsSettlement',
                title: '是否结算',
                formatter: function (value, row, index) {
                    if (value === 1) {
                        return "已结算";
                    }
                    else {
                        return "未结算";
                    }
                }
            },
            {
                field: 'fdResult',
                title: '战局结果',
                formatter: function (value, row, index) {
                    if (value === 0) {
                        return "战局未结束";
                    } else if (value === 1) {
                        return "发布者胜";
                    } else if (value === 2) {
                        return "挑战者胜";
                    } else if (value === -1) {
                        return "发布者缺席";
                    } else if (value === -2) {
                        return "挑战者缺席";
                    }
                }
            }
        ]],
        toolbar: '#tb'
    });
}
function showPic(value) {
    $('#win').window({
        width: 400,
        height: 300,
        modal: true,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        resizable: true,
        iconCls: "icon-add",
        content: "<img src='"+value+"' width='100%' height='100%'>",
        title: "战局截图"

    });
}
function edit() {
    var row = $('#dg').datagrid('getSelected');
    if (row != null) {
        var id = row.fdId;
        $('#win').window({
            width: 600,
            height: 400,
            modal: true,
            collapsible: false,
            minimizable: false,
            maximizable: false,
            resizable: true,
            iconCls: "icon-add",
            href: "/warSituationInfo/edit/" + id,
            title: "编辑"

        });
    } else {
        $.messager.alert('提示', '请选中一行数据操作!', 'Warning');
    }

}

function add() {
    $('#win').window({
        width: 600,
        height: 400,
        modal: true,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        resizable: true,
        iconCls: "icon-add",
        href: "add.jsp",
        title: "添加"

    });
}

function remove() {
    var row = $('#dg').datagrid('getSelections');
    if (row.length === 0) {
        $.messager.alert('提示', '请选中一行数据操作!', 'Warning');
    } else {
        var ids = new Array();
        $.each(row, function (i, row) {
            ids[i] = row['fdId'];
        });
        $.messager.confirm('提示', '确认删除该条记录?', function (r) {
            if (r) {
                console.log(ids);
                $.ajax({
                    type: 'POST',
                    url: '/warSituationInfo/remove',
                    data: {"ids": ids},
                    success: function (r) {
                        if (r.code == 0) {
                            $.messager.alert('提示', r.msg, 'info');
                            reLoad();
                            clearSelections();
                        } else {
                            $.messager.alert('提示', r.msg, 'Warning');
                        }
                    }
                });
            }
        });
    }
}

function submitForm() {
    $('#ff').form('submit', {
        onSubmit: function () {
            return $(this).form('enableValidation').form('validate');
        },
        success: function (data) {
            var r = JSON.parse(data);
            if (r.code == 0) {
                $.messager.alert("提示", r.msg, "info");
                closeWin();
                reLoad();
                clearSelections();
            } else {
                $.messager.alert("提示", r.msg, "Warning")
            }
        }
    });
}

function clearForm() {
    $('#ff').form('clear');
}

function reLoad() {
    initDg();
}

function closeWin() {
    $("#win").window("close");
}

function clearSelections() {
    $("#dg").datagrid("clearSelections");
}
